/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.basic.server.parser;

import android.util.Xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class Xml2ParseComment
{

	private Comment comments;

	public Xml2ParseComment(String s, int i)
		throws XmlPullParserException, IOException
	{
		comments = new Comment();
		comments.setBookid(i);
		XmlPullParser xmlpullparser = Xml.newPullParser();
		xmlpullparser.setInput(new ByteArrayInputStream(s.getBytes()), null);
		Parser(xmlpullparser);
	}

	private void Parser(XmlPullParser xmlpullparser)
		throws IOException, XmlPullParserException
	{
		OneComment onecomment = null;
		int eventType = xmlpullparser.getEventType();
	    
		boolean isCommentInfo = false;
	    while (eventType != XmlPullParser.END_DOCUMENT) {
	    	if (eventType == XmlPullParser.START_DOCUMENT) {
	    		//start parser
	    	} else if (eventType== XmlPullParser.START_TAG) {
	    		String tagName = xmlpullparser.getName();
	    		
	    		if ("commentlist".equals(tagName)) {
	    		} else if ("bookid".equals(tagName)) {
	    			xmlpullparser.next();
					comments.setBookid(Integer.valueOf(xmlpullparser.getText()).intValue());
	    		} else if ("score".equals(tagName)) {
	    			xmlpullparser.next();
					comments.setAveragescore(Integer.valueOf(xmlpullparser.getText()).intValue());
	    		} else if ("count".equals(tagName)) {
	    			xmlpullparser.next();
					comments.setCount(Integer.valueOf(xmlpullparser.getText()).intValue());
	    		} else if ("commentinfo".equals(tagName)) {
	    			onecomment = new OneComment();
	    		} else if ("time".equals(tagName)) {
	    			xmlpullparser.next();
					onecomment.setTime(Base64.decode((new StringBuilder(String.valueOf(xmlpullparser.getText()))).toString(), "utf-8"));
	    		} else if ("ip".equals(tagName)) {
	    			xmlpullparser.next();
					onecomment.setIp(Base64.decode((new StringBuilder(String.valueOf(xmlpullparser.getText()))).toString(), "utf-8"));
	    		} else if ("comment".equals(tagName)) {
	    			xmlpullparser.next();
					onecomment.setInfo(Base64.decode((new StringBuilder(String.valueOf(xmlpullparser.getText()))).toString(), "utf-8"));
	    		}
	    	} else if (eventType== XmlPullParser.END_TAG) {
	    		String tagName = xmlpullparser.getName();
	    		
	    		if ("commentlist".equals(tagName)) {
	    		} else if ("commentinfo".equals(tagName)) {
	    			comments.getList().add(onecomment);
	    		} 
	    	}
	    	
	    	eventType = xmlpullparser.next();
	    }
	}

	public Comment getCommentList()
	{
		return comments;
	}
}